// application.cpp
// Customizer
// Protea Customizer / Business Logic for managing applications
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "customizer.h"

//@ SEC 2
//@ CHA DelApplication
//@ COD DelApplication
//@ NUM 190
//@ CRI APPL_NBR
//@ TAB APPL_DEFN Write
//
//@ GEN DelApplication will delete an application record
//
TRANSACTION_CODE( DelApplication ) {
	XCBuffer* TransBuf = NULL;

	//@ GRS Start the transaction
	START_TXN( DelApplication );

		//@ Create format descriptor buffer
		TransBuf = App->CreateBuffer( FMT_APPL_DEFN, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();
			ApplDefnVerify( TransBuf, VERIFY_DELETE, &txnvars );
			txnvars.Errors->Check();

			ApplDefnDelete( TransBuf, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 1, &TransBuf );
	END_TXN();
	//@ GRE End the transaction
};





//
//@ CHA RevApplications
//@ COD RevApplications
//@ NUM 190
//@ CRI APPL_NBR
//@ TAB APPL_DEFN Read
//
//@ GEN RevApplications will list all application records
//
TRANSACTION_CODE( RevApplications ) {
	XCBuffer* TransBuf = NULL;
	XCBuffer* detail = NULL;
	XCDBStatement* stmt = NULL;

	XCSQLGen* query;

	//@ GRS Start the transaction
	START_TXN( RevApplications );

		//@ Create format descriptor buffer
		TransBuf = App->CreateBuffer( FMT_APPL_DEFN, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();
			query = txnvars.query;

			App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

			int child = txnvars.buffer->GetChildFormat();
			if (child) {
				detail = App->CreateBuffer( child, true );

				// Execute the query
				query->MakeSELECT( TransBuf );
				stmt = txnvars.dbtxn->ExecuteSQL( query );

				while (!stmt->Eof()) {
					detail->MoveCorresponding( TransBuf, txnvars.Errors, true );
					App->TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );
					stmt->MoveNext();
				};
			};

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 3, &TransBuf, &detail, &stmt );
	END_TXN();
	//@ GRE End the transaction
};





//
//@ CHA RevApplication
//@ COD RevApplication
//@ NUM 190
//@ CRI APPL_NBR
//@ TAB APPL_DEFN Read
//
//@ GEN RevApplications review a single application record
//
TRANSACTION_CODE( RevApplication ) {
	XCBuffer* TransBuf = NULL;
	XCDBStatement* stmt = NULL;
	XCSQLGen* query;

	//@ GRS Start the transaction
	START_TXN( RevApplication );

		//@ Create format descriptor buffer
		TransBuf = App->CreateBuffer( FMT_APPL_DEFN, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();
			query = txnvars.query;
			query->MakeSELECT( TransBuf );
			query->AddWHERE( 1, TransBuf->GetField( FLD_APPL_NBR ) );
			stmt = txnvars.dbtxn->ExecuteSQL( query );
		END_DB_TXN();

		txnvars.buffer->MoveCorresponding( TransBuf, txnvars.Errors );
		App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

	CLEANUP_TXN();
		App->CleanupObjects( 2, &TransBuf, &stmt );
	END_TXN();
	//@ GRE End the transaction
};




//
//@ CHA ChgApplication
//@ COD ChgApplication
//@ NUM 190
//@ CRI APPL_NBR
//@ TAB APPL_DEFN Write
//
//@ GEN ChgApplication will change an application record
//
TRANSACTION_CODE( ChgApplication ) {
	XCBuffer* TransBuf = NULL;

	//@ GRS Start the transaction
	START_TXN( ChgApplication );

		//@ Create format descriptor buffer
		TransBuf = App->CreateBuffer( FMT_APPL_DEFN, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();

			LOG_TRANSACTION();

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 1, &TransBuf );
	END_TXN();
	//@ GRE End the transaction
};







//
//@ CHA DelFullApplication
//@ COD DelFullApplication
//@ NUM 190
//@ CRI APPL_NBR
//@ TAB APPL_DEFN Write
//
//@ GEN DelFullApplication will delete an application record
//
TRANSACTION_CODE( DelFullApplication ) {
	XCBuffer* TransBuf = NULL;

	//@ GRS Start the transaction
	START_TXN( DelApplication );

		//@ Create format descriptor buffer
		TransBuf = App->CreateBuffer( FMT_APPL_DEFN, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();
			ApplDefnVerify( TransBuf, VERIFY_DELETE, &txnvars );
			txnvars.Errors->Check();

			//@ SUB ApplDefnDelete Delete application record
			ApplDefnDelete( TransBuf, &txnvars );

			//@ SUB DatabaseDefnDeleteAll Delete database records
			DatabaseDefnDeleteAll( TransBuf, &txnvars );

			//@ SUB FormatDescDeleteAll Delete format records (also deletes data structures, db_relate, dataset descr
			FormatDescDeleteAll( TransBuf, &txnvars );

			//@ SUB FieldDescDeleteAll Delete field records
			FieldDescDeleteAll( TransBuf, &txnvars );

			//@ SUB LiteralDefnDeleteAll Delete literal defn
			LiteralDefnDeleteAll( TransBuf, &txnvars );

			//@ SUB SystemValueDeleteAll Delete system values
			SystemValueDeleteAll( TransBuf, &txnvars );

			//@ SUB TransactionDeleteAll Delete transactions
			TransactionDeleteAll( TransBuf, &txnvars );

			//@ SUB UserExitDefnDeleteAll Delete User exits
			UserExitDefnDeleteAll( TransBuf, &txnvars );

			//@ SUB MessageDefnDeleteAll Delete Messages
			MessageDefnDeleteAll( TransBuf, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 1, &TransBuf );
	END_TXN();
	//@ GRE End the transaction
};
